package yxy.game.pm2.active.module;

import org.joda.time.DateTime;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.bean.actchallenge;
import yxy.game.pm2.active.ActiveUtil;
import yxy.game.pm2.active.module.actcontrol.activeControl;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.time.TimeChallengeGroup;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

import java.util.ArrayList;
import java.util.Map;

public class ActiveModule_限时挑战 extends PlayerModule {

	//bean ----------------------------------------------------
	public static final int acttype = 20;
	
	private int days;
	private int layer;
	private DateTime update;
	private ArrayList<Integer> ids = new ArrayList<Integer>();			//id组
	private ArrayList<Integer> victoryids = new ArrayList<Integer>();		//胜利id组
	private ArrayList<Integer> getrewardids = new ArrayList<Integer>();		//已经领取的id组
	
	public int getDays() {
		return days;
	}
	public void setDays(int days) {
		this.days = days;
	}
	public int getLayer() {
		return layer;
	}
	public void setLayer(int layer) {
		this.layer = layer;
	}
	public DateTime getUpdate() {
		return update;
	}
	public void setUpdate(DateTime update) {
		this.update = update;
	}
	public ArrayList<Integer> getIds() {
		return ids;
	}
	public void setIds(ArrayList<Integer> ids) {
		this.ids = ids;
	}
	public ArrayList<Integer> getVictoryids() {
		return victoryids;
	}
	public void setVictoryids(ArrayList<Integer> victoryids) {
		this.victoryids = victoryids;
	}
	public ArrayList<Integer> getGetrewardids() {
		return getrewardids;
	}
	public void setGetrewardids(ArrayList<Integer> getrewardids) {
		this.getrewardids = getrewardids;
	}
	
	//func ----------------------------------------------------

//	public static final int canget = 1;
//	public static final int hadget = 2;
//	public static final int cannotget = 3;
	
	/** 创建ActiveModule_限时挑战
	 * @param pcode
	 */
	public static ActiveModule_限时挑战 create(String pcode) {
		PlayerBean pb = PlayerBean.get(pcode);
		int sid = pb.getServerId();

		ServerBean sb = ServerBean.get(sid);
		int areaid = sb.getAreaId();
		
		activeControl sai = activeControl.get(areaid, acttype);
		if(sai == null) {
			return null;
		}
		
		int layer = 1;
		
		ActiveModule_限时挑战 am = new ActiveModule_限时挑战();
		am.setDays(1);
//		am.setTimeline(sai.getTimeline());
		am.setPlayerCode(pcode);
		am.setLayer(layer);
		am.setUpdate(DateTime.now());
		
		Map<String, actchallenge> execlmap = ActDataCenter.getAllData(actchallenge.class, pcode);
		for(actchallenge execl : execlmap.values()) {
			if(execl.getLayer() == layer) {
				am.getIds().add(execl.getId());
			}
		}
		
		am.set();
		return am;
	}

	public void update() {

		int days = ActiveUtil.days(getUpdate(), DateTime.now());
		if(days >= 1) {

			int newdays = getDays() + 1;
			
			setUpdate(DateTime.now());
			setDays(newdays);
			
			ArrayList<Integer> groups = new ArrayList<Integer>();
			Map<String, actchallenge> execlmap = ActDataCenter.getAllData(actchallenge.class, getPlayerCode());
			for (actchallenge execl : execlmap.values()) {
				if(!groups.contains(execl.getLayer())) {
					groups.add(execl.getLayer());
				}
			}
			
			int newlayer = getLayer() + 1;

			setLayer(newlayer);
			setIds(new ArrayList<Integer>());
			setVictoryids(new ArrayList<Integer>());
			setGetrewardids(new ArrayList<Integer>());
			
			for(actchallenge execl : execlmap.values()) {
				if(execl.getLayer() == getLayer()) {
					getIds().add(execl.getId());
				}
			}
			
			set();
		}
	}
	
	// redis ---------------------------------------------------------

	static private String key(String playerCode) {
		return String.format("PLAYER:%s:ACTIVE:%S", playerCode, acttype);
	}

	public void set() {
		try {
			setv(key(getPlayerCode()), this);
			check();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取 ActiveModule_限时挑战
	 * 
	 * @param playerCode
	 * @return
	 */
	static public ActiveModule_限时挑战 get(String playerCode, Boolean isNewPlayer) {
		try {
			ActiveModule_限时挑战 module = getv(key(playerCode));

			//新用户不需要去查数据库
			if (module == null && !isNewPlayer) {
				module = MySQLCacheDAO.getV(key(playerCode), ActiveModule_限时挑战.class);
				if(module != null){module.set();}//同步到redis
			}
			if (module == null) {
				module = create(playerCode);
			}
			if(module == null){return null;}

			module.update();
			module.check();
			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/** 查看是否有可领取奖励 控制红点 */
	public void check() {
		if (getDays() > TimeChallengeGroup.ChallengeGroup() || getIds().size() == 0) {
			new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.关闭);
			new TipHandle().updateTip(getPlayerCode(), TipType.活动_限时挑战, false);
			return;
		}

		if (getGetrewardids().size() == getIds().size()) {
			new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.领完了);
			new TipHandle().updateTip(getPlayerCode(), TipType.活动_限时挑战, false);
		} else {
			new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.开启);
			new TipHandle().updateTip(getPlayerCode(), TipType.活动_限时挑战, true);
		}
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				ActiveModule_限时挑战 activeModule_限时挑战 = getv(key(playerCode));
				MySQLCacheDAO.setV(key, activeModule_限时挑战);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}
	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	}

}
